﻿using MediatR;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.Purchase;
using RedDragon.Common.Dtos;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.Purchases
{
    /// <summary>
    /// 根据采购/委外订单编号获取订单行信息并打印
    /// </summary>
    public class StampOrderBankHandler : IRequestHandler<StampOrderBankCommand, Result<List<OrderBankDto>>>
    {
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<StampOrderBankHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="logger"></param>
        public StampOrderBankHandler(ISqlSugarClient db,
            ILogger<StampOrderBankHandler> logger)
        {
            _db = db;
            _logger = logger;
        }

        /// <summary>
        /// 根据采购/委外订单编号获取订单行信息并打印
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<OrderBankDto>>> Handle(StampOrderBankCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<OrderBankDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var query = await _db.Queryable<OrderBanks, MaterialManagement, Units>((o, m, u) => o.MaterialId == m.MaterialId && o.UnitId == u.UnitId)
                    .Select((o, m, u) => new OrderBankDto()
                    {
                        OrderBankId = o.OrderBankId,
                        MaterialId = o.MaterialId,
                        Unitprice = o.Unitprice,
                        Quantity = o.Quantity,
                        UnitId = o.UnitId,
                        BankAmount = o.BankAmount,
                        Abstract = o.Abstract,
                        PurchaseNumber = o.PurchaseNumber,
                        CreatedOwner = o.CreatedOwner,
                        CreatedTime = o.CreatedTime,
                        UpdateOwner = o.UpdateOwner,
                        UpdateTime = o.UpdateTime,
                        ExamineOwner = o.ExamineOwner,
                        ExamineTime = o.ExamineTime,
                        MaterialCode = m.MaterialCode,
                        MaterialName = m.MaterialName,
                        UnitName = u.UnitName,
                    }).Where(o => o.PurchaseNumber == request.PurchaseNumber).ToListAsync();
                if (query.Count > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = query;
                    result.Msg = "根据采购/委外订单编号获取订单行信息并打印成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "根据采购/委外订单编号获取订单行信息并打印异常" + ex.Message;
                _logger.LogError("根据采购/委外订单编号获取订单行信息并打印异常" + ex.Message);
            }
            return result;
        }
    }
}
